// pages/visit/add/add.js
var visitservice = require("../../../apiservice/visit/visitservice");
var util = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    visitItem: {
      subject: '', scheduledStart: '请选择', new_visitpurpose: ''
      , requiredattendees: [], requiredattendeesName: '请选择'
    },
    doctorId:null,
   // disabled:''
    startDate: new Date().getTime(), //util.frmatDate(new Date()),
    visitDateShow: false,
    visitDate:''
  },

  /**
   * 保存信息
   */
  onSave: function () {
    if(!this.checkAddInfo()){
      return false;
    }
    wx.showLoading({
      title:'添加拜访记录...',
      mask:true
    }) 
    var appointment = this.data.visitItem;
    var that = this;
	if(this.data.doctorId!=null){
		var requiredattendees =[];
		requiredattendees.push(this.data.doctorId);
		appointment.requiredattendees = requiredattendees;
	}
	
    visitservice.AddAppointment(appointment).then(function (success) {
      console.log(success);
      //还原状态
      that.setData({
        visitItem: {
          subject: null, scheduledStart: null, new_visitpurpose: null
          , requiredattendees: [], requiredattendeesName: '请选择'
        }
      })
      wx.hideLoading();
      wx.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 2000
      });
      

    }).catch(function (ex) {
      console.log(ex);
      wx.hideLoading();
      wx.showToast({
        title: '添加失败',
        icon: 'none',
        duration: 2000
      });

    })
  },
  /**
   * 控件赋值
   * @param {*} e 
   */
  onTimeChange: function (e) {
    //console.log(e);
    this.setData({
      ['visitItem.scheduledStart']: e.detail.value
    });
    //console.log(this.data.visitItem);
  },
  onSubjectChange: function (e) {
    this.setData({
      ['visitItem.subject']: e.detail
    });
  },
  onPurposeChange: function (e) {
    this.setData({
      ['visitItem.new_visitpurpose']: e.detail
    });
    //console.log(this.data.visitItem);
  },
  toDoctorPage: function () {
    var that = this;
    wx.navigateTo({
      url: "../../doctor/list/list",
      events: {
        openedDoctorPage: function (e) {
          //console.log(e.data)
        },
        DoctorEvent: function (data) {
          console.log(data)
          var requiredattendees =[];
          requiredattendees.push(data.doctorid);
          that.setData({
            ['visitItem.requiredattendees']: requiredattendees,
            ['visitItem.requiredattendeesName']: data.doctorname
          });
        }
      },
      success: function (res) {
        res.eventChannel.emit('openedDoctorPage', { data: 'choose' })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options){
      this.setData({
        doctorId:options.contactid,
        ['visitItem.requiredattendeesName']:options.doctorName
      })
    }
    // this.setData({startDate:util.frmatDate(new Date())});
    // console.log(this.data.startDate);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkAddInfo(){
    var subject = this.data.visitItem.subject;
    var scheduledStart = this.data.visitItem.scheduledStart;
    var requiredattendeesName =this.data.visitItem.requiredattendeesName;
    if(null ==subject||''==subject){
      wx.showToast({
        title:'请填写主题',
        icon:'none',
        duration:1500
      })
      return false;
    }
    if(null ==scheduledStart||'请选择'==scheduledStart){
      wx.showToast({
        title:'请选择计划拜访时间',
        icon:'none',
        duration:1500
      })
      return false;
    }
    if(requiredattendeesName=='请选择'||!requiredattendeesName){
      wx.showToast({
        title:'请选择医生',
        icon:'none',
        duration:1500
      })
      return false;
    }
    return true;
  },
  selectVisitDate:function(e){
    this.setData({ visitDateShow:true});
  }, 
  onVisitDateConfirm:function(e){
    var time = e.detail;

    this.setData({
      visitDateShow: false,
      visitDate: time
    })
    var frmatDate = util.frmatDate(new Date(this.data.visitDate));
    this.setData({ ['visitItem.scheduledStart']: frmatDate}); 
  },
  onVisitCancel:function(e){
    this.setData({ visitDateShow: false });
  },
  
})